/*-------------------------------------------------------------------------
 * CLR版本:     4.0.30319.42000
 * 创建人:      song
 * 创建时间：    2024/4/9 14:47:37
 * 功 能：      Fps
 *-------------------------------------------------------------------------*/

using System;
using UnityEngine;

namespace SCore.UI
{
    public class SFPSCounter : MonoBehaviour
    {
        public float fpsMeasuringDelta = 1.0f;

        private float _timePassed;
        private int _frameCount = 0;
        private float _fps = 0.0f;

        private void Start()
        {
            _timePassed = 0.0f;
        }

        private void Update()
        {
            _frameCount = _frameCount + 1;
            _timePassed = _timePassed + Time.deltaTime;

            if (_timePassed > fpsMeasuringDelta)
            {
                _fps = _frameCount / _timePassed;

                _timePassed = 0.0f;
                _frameCount = 0;
            }
        }

        private void OnGUI()
        {
            GUIStyle bb = new GUIStyle();
            bb.normal.background = null; //这是设置背景填充的
            bb.normal.textColor = new Color(0f / 255, 196f / 255, 0f / 255, 0.77f); //设置字体颜色的
            bb.fontSize = 22; //当然，这是字体大小

            //居中显示FPS
            GUI.Label(new Rect(Screen.width / 50, 95 * Screen.height / 100, 120, 30),
                "FPS: " + Math.Round(_fps, 2), bb);
        }
    }
}